namespace Todos.Application.TodoLists.Commands;

[AuthConfig(typeof(TodoList), ResourceAccessType.RoleLimit, "Delete")]
public class DeleteTodoListCommand : IRequest<ApiResult>, ICommand
{
    public long[] Ids { get; set; } = [];
}

public class DeleteTodoListCommandHandler(IServiceProvider provider) : IRequestHandler<DeleteTodoListCommand, ApiResult>
{
    public async Task<ApiResult> Handle(DeleteTodoListCommand request, CancellationToken cancellationToken)
    {
        var repository = provider.GetRepository<TodoList>();
        var spec = new TodoListByIdsSpec(request.Ids);
        await repository.DeleteBatchAsync(spec, cancellationToken);

        return new ApiResult(ApiResultType.Success, $"待办列表 {request.Ids.ExpandAndToString(m => m.ToString())} 删除成功");
    }
}

public class DeleteTodoListCommandValidator : AbstractValidator<DeleteTodoListCommand>
{
    public DeleteTodoListCommandValidator()
    {
        RuleFor(v => v.Ids).NotEmpty().WithMessage("请至少选择一个待办列表进行删除");
    }
}